﻿using GalaSoft.MvvmLight;

namespace Budgetsch.WP7.ViewModel.Setting
{
    using System.Windows;
    using System.Windows.Input;

    using Budgetsch.WP7.Navigation;
    using Budgetsch.WP7.Utils;
    using Budgetsch.WP7.WP7DataService;

    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class SettingsViewModel : ViewModelBase
    {
        private readonly CookieContainerViewModel _cookieContainerViewModel;

        private readonly AppSettings _appSettings;

        /// <summary>
        /// The <see cref="PublicityToggleButtonViewModel" /> property's name.
        /// </summary>
        public const string PublicityToggleButtonViewModelPropertyName = "PublicityToggleButtonViewModel";

        private ToggleButtonViewModel _publicityToggleButtonViewModel;

        /// <summary>
        /// Sets and gets the PublicityToggleButtonViewModel property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ToggleButtonViewModel PublicityToggleButtonViewModel
        {
            get
            {
                return _publicityToggleButtonViewModel;
            }

            set
            {
                if (_publicityToggleButtonViewModel == value)
                {
                    return;
                }

                _publicityToggleButtonViewModel = value;
                RaisePropertyChanged(PublicityToggleButtonViewModelPropertyName);
            }
        }
        /// <summary>
        /// The <see cref="IsBusy" /> property's name.
        /// </summary>
        public const string IsBusyPropertyName = "IsBusy";

        private bool _isBusy;

        /// <summary>
        /// Sets and gets the IsBusy property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }

            set
            {
                if (_isBusy == value)
                {
                    return;
                }

                _isBusy = value;
                RaisePropertyChanged(IsBusyPropertyName);
            }
        }

        public ICommand ChangePublicityCommand { get; set; }

        /// <summary>
        /// Initializes a new instance of the SettingsViewModel class.
        /// </summary>
        public SettingsViewModel(CookieContainerViewModel cookieContainerViewModel)
        {
            _cookieContainerViewModel = cookieContainerViewModel;


            ChangePublicityCommand = new RelayCommand<bool>(p =>
                {
                    IsBusy = true;
                    var client = new WP7DataServiceClient { CookieContainer = _cookieContainerViewModel.CookieContainer };
                    client.SetPublicityCompleted += (s, ev) =>
                        {
                            IsBusy = false;
                            PublicityToggleButtonViewModel.IsChecked = ev.Error != null ? PublicityToggleButtonViewModel.IsChecked : p;
                        };
                    client.SetPublicityAsync(p);

                });
            Messenger.Default.Register<LoadSettingsViewMessage>(this, m =>
                {
                    IsBusy = true;
                    var client = new WP7DataServiceClient { CookieContainer = _cookieContainerViewModel.CookieContainer };
                    client.IsSharedUserCompleted += (s, e) =>
                        {
                            IsBusy = false;
                            if (e.Error != null)
                            {
                                MessageBox.Show("Can't change the publicity property", "Error", MessageBoxButton.OK);
                            }
                            else
                            {
                                PublicityToggleButtonViewModel.IsChecked = e.Result;
                            }
                        };
                    client.IsSharedUserAsync();
                });
            PublicityToggleButtonViewModel = new ToggleButtonViewModel("Profile publicity", false);
        }
    }
}